package com.apexedu.portal.service.impl;
/**
 * 管理员模板控�?
 * @author zhuzhonglei
 * @time 2009-11-20
 */

import com.apexedu.biz.Const;
import com.apexedu.framework.service.impl.AbstractBaseServiceSupport;
import com.apexedu.framework.util.DBUtil;
import com.apexedu.framework.util.Pager;
import com.apexedu.framework.util.SeqFactory;
import com.apexedu.identity.action.IdLoginAction;
import com.apexedu.portal.action.MessageFactorybaseAction;
import com.apexedu.portal.entity.TUserMessagefactory;
import com.apexedu.sys.util.BusinessLogUtil;
import com.apexedu.util.StringUtil;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("messageFactorybaseService")
public class MessageFactorybaseServiceImpl extends AbstractBaseServiceSupport {

    private static final Logger logger = Logger.getLogger(MessageFactorybaseServiceImpl.class);

    public List<?> list(Object obj, Pager pager) throws Exception {
        long c=DBUtil.count("select count(*) as c from T_USER_MESSAGEFACTORY t ");
        pager.setTotalRows(c);
        System.out.println("messageFactorybaseServiceImpl"+c);
        String sql =" select wid,(select code from t_db where wid=t.code) as code,messagename,applicablerole,shuom,state " +
                    " from T_USER_MESSAGEFACTORY t";
        List<TUserMessagefactory> list = DBUtil.queryPageBeanList(pager,sql, TUserMessagefactory.class);
        return list;
    }

    @Override
    public void saveOrUpdate(Object myaction) throws Exception {
        MessageFactorybaseAction action = (MessageFactorybaseAction) myaction;
        TUserMessagefactory messagefactory = action.getTuserMessagefactory();
        if (messagefactory != null) {
            if (StringUtil.isNotBlank(messagefactory.getApplicablerole())){
                messagefactory.setApplicablerole(messagefactory.getApplicablerole().replace(" ", ""));
            }
            messagefactory.setApplicablerole(messagefactory.getUsrtype());
            if (StringUtil.isNotBlank(messagefactory.getWid())) {
                baseDao.updateNotNull(messagefactory);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_USER_MESSAGEFACTORY, Const.LOG_ACTION_UPDATE,
                        IdLoginAction.getIpAddr(action.getRequest()), "修改信息工厂");
            } else {
                messagefactory.setWid(SeqFactory.getNewSequenceAlone());
                baseDao.save(messagefactory);
                BusinessLogUtil.log(action.getUserloginid(), Const.CZDX_T_USER_MESSAGEFACTORY, Const.LOG_ACTION_SAVE,
                        IdLoginAction.getIpAddr(action.getRequest()),"新增信息工厂");
            }
        }
    }

    @Override
    public void load(Object myaction) throws Exception {
        MessageFactorybaseAction action = (MessageFactorybaseAction) myaction;
        String wid = action.getWid();
        String sql=" select * " +
                   " from T_USER_MESSAGEFACTORY tsr " +
                   " where tsr.wid = ?";
        TUserMessagefactory messagefactory = DBUtil.queryBean(sql, TUserMessagefactory.class, wid);
        if (StringUtil.isNotBlank(messagefactory.getApplicablerole())){
            messagefactory.setApplicablerole(messagefactory.getApplicablerole().replace(" ",""));
        }
        action.setTuserMessagefactory(messagefactory);
    }

    @Override
    public boolean remove(Object myaction) throws Exception {
        return false;
    }
}
